package com.shm.cmb;

import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;

/**
 * 给定入栈顺序，求所有可能的出栈顺序。
 * @author SHM
 */
public class Main {
    static int count=1;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String s = sc.nextLine();
        char[] chars = s.toCharArray();

        fun(chars,0,new Stack<Character>(),"");
//        new TreeMap<>();
    }

    public static void fun(char[] chars,int n,Stack<Character> stack,String str){
        if (n==chars.length&&stack.isEmpty()){
            System.out.print(str);

            if (count%3==0){
                System.out.println();
            }else {
                System.out.print(",");
            }
            count++;
        }else {
//            Stack<Character> stack1 = (Stack<Character>)stack.clone();
//            Stack<Character> stack2 = (Stack<Character>)stack.clone();
            Stack<Character> stack1 = new Stack<Character>();
            stack1.addAll(stack);
            Stack<Character> stack2 = new Stack<Character>();
            stack2.addAll(stack);
            //选择入栈
            if(n<chars.length){
                //如果未全部入栈
                stack1.push(chars[n]);
                //继续入栈
                fun(chars,n+1,stack1,str);
            }

            //选择出栈
            if (!stack2.isEmpty()){
                //如果栈不空，且入栈元素不是最后一个
                //如果是最后一个，上面已经先执行出栈了
                //记录出栈元素
                String temp  = str+stack2.peek();
                stack2.pop();
                fun(chars,n,stack2,temp);
            }
        }
    }
}
